/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

import QtQuick 2.15
import QtQml 2.15
import QtQuick.Layouts 1.15
import QtQuick.Templates 2.15 as T
import QtGraphicalEffects 1.15
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PC3
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.kirigami 2.13 as Kirigami
import org.kde.kcoreaddons 1.0 as KCoreAddons
import org.kde.kquickcontrolsaddons 2.0 as KQuickAddons

PC3.ToolBar {
    id: root

    property alias searchText: searchField.text
    property real preferredNameAndIconWidth: 0
    property real scaleFactor: PlasmaCore.Units.devicePixelRatio;
    property QtObject searchField: searchField
    contentHeight: searchField.implicitHeight
    contentWidth: searchField.implicitWidth

    leftPadding: 0
    rightPadding: 0
    //topPadding: Math.round((background.margins.top - background.inset.top) / 2.0)
    //bottomPadding: background.margins.bottom + Math.round((background.margins.bottom - background.inset.bottom) / 2.0)

    background: Rectangle {
        //color: "red"
        opacity: 0
    }

    spacing: plasmoid.rootItem.backgroundMetrics.spacing
    function select(start, end) {
        searchField.select(start, end)
    }

    function clear() {
        searchField.clear()
    }

    RowLayout {
        id: rowLayout
        spacing: root.spacing
        height: parent.height
        anchors {
            left: parent.left
            leftMargin: 10
            right: parent.right
        }
        Keys.onDownPressed: plasmoid.rootItem.contentArea.forceActiveFocus(Qt.TabFocusReason)

        PlasmaExtras.SearchField {
            id: searchField
            Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
            Layout.fillWidth: true
            Layout.rightMargin: 10
            implicitHeight: 36 * scaleFactor
            implicitWidth: 325 * scaleFactor
            background: Rectangle {
                radius: 8
                color: "#0E0F0F"
                opacity: 0.1
            }

            //Layout.leftMargin: plasmoid.rootItem.backgroundMetrics.leftPadding
            focus: true

            Binding {
                target: plasmoid.rootItem
                property: "searchField"
                value: searchField
                // there's only one header ever, so don't waste resources
                restoreMode: Binding.RestoreNone
            }
            Connections {
                target: plasmoid
                function onExpandedChanged() {
                    if (plasmoid.expanded) {
                        searchField.clear()
                    }
                }
            }
            onTextEdited: {
                searchField.forceActiveFocus(Qt.ShortcutFocusReason)
            }
            onAccepted: {
                plasmoid.rootItem.contentArea.currentItem.action.triggered()
                plasmoid.rootItem.contentArea.currentItem.forceActiveFocus(Qt.ShortcutFocusReason)
            }
            Keys.priority: Keys.AfterItem
            Keys.forwardTo: plasmoid.rootItem.contentArea !== null ? plasmoid.rootItem.contentArea.view : []
            Keys.onLeftPressed: if (activeFocus) {
                                    if (LayoutMirroring.enabled) {
                                        nextItemInFocusChain().forceActiveFocus(Qt.TabFocusReason)
                                    } else {
                                        nextItemInFocusChain(false).forceActiveFocus(Qt.BacktabFocusReason)
                                    }
                                }
            Keys.onRightPressed: if (activeFocus) {
                                     if (!LayoutMirroring.enabled) {
                                         nextItemInFocusChain().forceActiveFocus(Qt.TabFocusReason)
                                     } else {
                                         nextItemInFocusChain(false).forceActiveFocus(Qt.BacktabFocusReason)
                                     }
                                 }
        }
    }
}
